<?php
namespace src;
use classlib\input;
use src\logic\Cadmin;
use classlib\core as C;
use classlib\core\controller;
use classlib\core\lang;
use src\logic\lmailer;
class Cemail extends controller {
	public function main() {
		$view  = C\view::inst('extjs');
		Cadmin::chkaccess($view);
		$dlang = lang::inst('dd');
		$view->assign('yes2no', $dlang->get('sh0007'));
		$view->assign('visok', $dlang->get('sh0006'));
		$view->assign('status', $dlang->get('log0001'));
		$view->assign_by_ref('title', Cadmin::$name);
		$view->display('live/log/email.ext.js');
	}
	
	/**
	 * @desc    发送邮件资料信息
	 * @doc     发送邮件
	 * @access  需要授权
	 */
	public function dosend() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$where = array('status'=>1);
		$smtps = C\model::load('main.sys.mailsets')->get_list(0, -1, $where);
		if (empty($smtps)) {
			$view->assign_error(1, '没有找到可用的smtp服务');
		}
		$ids   = input::post('ids');
		$ids   = explode(',', $ids);
		$ids   = array_filter($ids, 'is_numeric');
		if (empty($ids)) {
			$view->assign_error(2, '请选择要发送重发的邮件记录');
		}
		foreach ($ids as $id) {
			$item = C\model::load('main.sys.mailsends')->get_one($id);
			if (empty($item)) {
				continue;
			}
			$sdata  = array('status'=>2, 'trytime'=>0, 'sndtime'=>C\config::$nowtime);
			$idx    = 0;
			for ($i = 0; $i < 3; $i++) {
				$idx  = array_rand($smtps, 1);
				$sdata['trytime'] += 1;
				if (lmailer::dosend($item['receiver'], $item['title'], $item['msg'], $smtps[$idx])) {
					$sdata['status'] = 1;
					break;
				}
			}
			$sdata['mailid'] = $smtps[$idx]['id'];
			C\model::load('main.sys.mailsends')->save($id, $sdata);
		}
		$view->assign('msg', '邮件发送处理完成！');
		$view->display();
	}
	
	public function dolist() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		list($start, $limit, $sort) = Cadmin::offset();
		$where  = array();
		$name   = trim(input::post('name'));
		if ($name) {
			$where[] = array('title', '%'.$name.'%', \classlib\orm::OP_LIKE);
		}
		$email  = trim(input::post('email'));
		if ($email) {
			$where[] = array('receiver', '%'.$email.'%', \classlib\orm::OP_LIKE);
		}
		$status = input::post('status');
		if (is_numeric($status)) {
			$where[] = array('status', $status, \classlib\orm::OP_EQ);
		}
		$sdate  = input::post('sdate');
		if ($sdate) {
			$where[] = array('stime', strtotime($sdate), \classlib\orm::OP_GE);
		}
		$edate  = input::post('edate');
		if ($edate) {
			$where[] = array('stime', strtotime($edate), \classlib\orm::OP_LE);
		}

		$orm    = C\model::load('main.sys.mailsends');
		$orm->set_ver();
		$total  = $orm->get_total($where);
		$list   = array();
		if ($total) {
			$list = $orm->get_list($start, $limit, $where, '*', $sort);
			foreach ($list as &$item) {
				$item['stime']   = date('Y-m-d H:i:s', $item['stime']);
				$item['sndtime'] = ($item['sndtime'] > 0)? date('Y-m-d H:i:s', $item['sndtime']):'';
			}
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
	
	/**
	 * @desc    删除邮件SMTP记录信息
	 * @doc     删除SMTP
	 * @access  需要授权
	 */
	public function dodelete() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$ids  = input::post('ids');
		$ids  = explode(',', $ids);
		if(!is_array($ids)) {
			$ids = array($ids);
		}
		$ids  = array_filter($ids, 'is_numeric');
		if(empty($ids)) {
			$view->assign_error('1', '请选择要删除的记录');
		}
		foreach ($ids as $id) {
			C\model::load('main.sys.mailsets')->delete($id);
		}
		$view->display();
	}
	
	/**
	 * @desc    保存SMTP资料信息
	 * @doc     保存SMTP
	 * @access  需要授权
	 */
	public function dosave() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$record = array();
		$filter = new \classlib\validator\auditor();
		$lang   = C\lang::inst('adm');
		$filter->lang($lang);
		try {
			$record['user'] = $filter->reset('user')->required()->maxlen(63)->run();
			$record['from'] = $filter->reset('from')->required()->email()->run();
			$record['fromname']= $filter->reset('fromname')->required()->maxlen(63)->run();
			$record['loginpw'] = $filter->reset('loginpw')->required()->maxlen(64)->run();
		} catch (\classlib\validator\AuditorException $e) {
			$view->assign_error($e->getField(), $e->getMessage());
		} catch (\Exception $e) {
			$view->assign_error($e->getCode(), $e->getMessage());
		}
		$record['stmp'] = input::post('stmp');
		$record['port'] = input::post('port');
		if (!is_numeric($record['port']) || $record['port'] < 1) {
			$record['port'] = 25;
		}
		$record['isauth'] = (int)input::post('isauth');
		$record['status'] = (int)input::post('status');
		$id   = input::post('id');
		$id	  = intval($id);
		if($id > 0) {
			C\model::load('main.sys.mailsets')->save($id, $record);
		} else {
			$record['stime'] = C\config::$nowtime;
			C\model::load('main.sys.mailsets')->new_one($record);
		}
		$view->display();
	}
	
	/**
	 * @desc    管理SMTP资料信息
	 * @doc     管理SMTP
	 * @access  需要授权
	 */
	public function dosmtp() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		list($start, $limit) = Cadmin::offset();
		$where  = array();
		$user   = trim(input::post('user'));
		if ($user) {
			$where[] = array('user', '%'.$user.'%', \classlib\orm::OP_LIKE);
		}
		$status = input::post('status');
		if (is_numeric($status)) {
			$where[] = array('status', $status, \classlib\orm::OP_EQ);
		}
		$orm    = C\model::load('main.sys.mailsets');
		$total  = $orm->get_total($where);
		$list   = array();
		if ($total > 0) {
			$list = $orm->get_list($start, $limit, $where);
			foreach ($list as &$item) {
				$item['stime'] = date('Y-m-d H:i:s', $item['stime']);
			}
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
}
